package net.anzhuo.controller.db.report;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.HttpUtils;
import net.anzhuo.entity.GsAssetsliab;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;
import net.anzhuo.entity.GsBusinessProfits;
import net.anzhuo.entity.GsCashFlow;
import net.anzhuo.entity.GsCertificates;
import net.anzhuo.entity.GsChange;
import net.anzhuo.entity.GsContracts;
import net.anzhuo.entity.GsCorpothersinfo;
import net.anzhuo.entity.GsEmployees;
import net.anzhuo.entity.GsGaoGuan;
import net.anzhuo.entity.GsGudong;
import net.anzhuo.entity.GsHonors;
import net.anzhuo.entity.GsReportLog;
import net.anzhuo.entity.GsZiZhi;
import net.anzhuo.entity.report.Branch;
import net.anzhuo.entity.report.ChangeRecord;
import net.anzhuo.entity.report.ContractExtraction;
import net.anzhuo.entity.report.EmployeesSituation;
import net.anzhuo.entity.report.Honor;
import net.anzhuo.entity.report.LrRecord;
import net.anzhuo.entity.report.Qualifications;
import net.anzhuo.entity.report.Senior;
import net.anzhuo.entity.report.Shareholder;
import net.anzhuo.entity.report.SystemAuthentication;
import net.anzhuo.entity.report.UploadLocalData;
import net.anzhuo.entity.report.XjllRecord;
import net.anzhuo.entity.report.ZcfzRecord;

import net.anzhuo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 评级报告接口
 */
@RestController
@Slf4j
@RequestMapping("/reportUpload/")
public class ReportTestController {

    @Autowired
    private GsBaseService gsBaseService;
    @Autowired
    private GsChangeService gsChangeService;
    @Autowired
    private GsBranchService gsBranchService;
    @Autowired
    private GsGudongService gsGudongService;
    @Autowired
    private GsCorpothersinfoService gsCorpothersinfoService;
    @Autowired
    private GsZiZhiService gsZiZhiService;
    @Autowired
    private GsReportLogService gsReportLogService;
    @Autowired
    private GsBusinessProfitsService gsBusinessProfitsService;
    @Autowired
    private GsCashFlowService gsCashFlowService;
    @Autowired
    private GsAssetsliabService gsAssetsliabService;
    @Autowired
    private GsGaoGuanService gsGaoGuanService;
    @Autowired
    private GsContractsService gsContractsService;
    @Autowired
    private GsEmployeesService gsEmployeesService;
    @Autowired
    private GsHonorsService gsHonorsService;
    @Autowired
    private GsCertificatesService gsCertificatesService;

    private  Integer updateIndex = 0;
    private Integer insertIndex  = 0 ;


    private String dealBigDecimal(BigDecimal value) {
        DecimalFormat df2 = new DecimalFormat("###0.00");
        if (value == null) {
            return "—";
        } else {
            return df2.format(value);
        }
    }


    @RequestMapping(value="insert",method = RequestMethod.POST)
    @ResponseBody
    public boolean insert(@RequestBody UploadLocalData uploadLocalData) throws Exception {

        Map<Object, String> returnMap = new HashMap<>();

        System.out.println("--------------" + JSON.toJSONString(uploadLocalData));
        Long time = System.currentTimeMillis();
        /**基本信息表*/
        GsBase gsBase = new GsBase();
        List<GsBase> companyNameList = gsBaseService.getByNameList(uploadLocalData.getComponyBasicInfo().getComponyName());
        GsBase companyName=null;
        if(companyNameList.size()>0)
        {
        	companyName=companyNameList.get(0);
        	for(int i=1;i<companyNameList.size();i++)
        	{
        		gsBaseService.delete(companyNameList.get(i).getId());
        	}
        }
        GsBase creditCode=null;
        if(!StringUtils.isEmpty(uploadLocalData.getComponyBasicInfo().getCreditCode()))
        {
        	
       
        
        List<GsBase> creditCodeList = gsBaseService.getByCreditCodeList(uploadLocalData.getComponyBasicInfo().getCreditCode());
   
        
        if(creditCodeList.size()>0&&uploadLocalData.getComponyBasicInfo().getCreditCode().length()==18)
        {
        	creditCode=creditCodeList.get(0);
        	for(int i=1;i<creditCodeList.size();i++)
        	{
        		if(creditCodeList.get(i).getId()!=null)
        		{
        	gsBaseService.delete(creditCodeList.get(i).getId());
        		}
        	}
        }
        }
        log.debug("查询用了" + String.valueOf(System.currentTimeMillis()-time/1000));


        if (companyName != null || creditCode != null) {

            updateIndex ++ ;

            if (companyName != null) {
                gsBase = companyName;
            }
            if (creditCode != null) {
                gsBase = creditCode;
            }
            try {
                gsBase.setName(uploadLocalData.getComponyBasicInfo().getComponyName());
                gsBase.setCreditCode(uploadLocalData.getComponyBasicInfo().getCreditCode());
                gsBase.setLegal(uploadLocalData.getComponyBasicInfo().getFr());
                gsBase.setRegAddress(uploadLocalData.getComponyBasicInfo().getAdress());
                gsBase.setEnterpriseType(uploadLocalData.getComponyBasicInfo().getEconomicType());
                gsBase.setRegZb(uploadLocalData.getComponyBasicInfo().getRegistMoney());
                gsBase.setTel(uploadLocalData.getComponyBasicInfo().getContactTel());
                gsBase.setOperScope(uploadLocalData.getComponyBasicInfo().getScope());
                gsBase.setYear(String.valueOf(uploadLocalData.getComponyBasicInfo().getEstablishment()));
                gsBase.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                if(uploadLocalData.getComponyBasicInfo().getCreateTime()!=null){
                    gsBase.setRegTime(new SimpleDateFormat("yyyy-MM-dd").format(uploadLocalData.getComponyBasicInfo().getEstablishment()));
                }else{
                    gsBase.setRegTime("-");
                }
                gsBaseService.update(gsBase);
            } catch (Exception e) {
                log.error("",e);
                log.debug("更新异常原因"+ JSON.toJSON(uploadLocalData));
                return false;
            }
        } else if (companyName == null && creditCode == null) {
            try {
                insertIndex ++ ;
                gsBase.setName(uploadLocalData.getComponyBasicInfo().getComponyName());
                gsBase.setLegal(uploadLocalData.getComponyBasicInfo().getFr());
                gsBase.setRegAddress(uploadLocalData.getComponyBasicInfo().getAdress());
                gsBase.setEnterpriseType(uploadLocalData.getComponyBasicInfo().getEconomicType());
                gsBase.setCreditCode(uploadLocalData.getComponyBasicInfo().getCreditCode());
                gsBase.setRegZb(uploadLocalData.getComponyBasicInfo().getRegistMoney());
                gsBase.setTel(uploadLocalData.getComponyBasicInfo().getContactTel());
                gsBase.setOperScope(uploadLocalData.getComponyBasicInfo().getScope());
                gsBase.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                String coMd5=UUID.randomUUID().toString().replace("-", "");
                gsBase.setMd5(coMd5);
                gsBase.setYear(String.valueOf(uploadLocalData.getComponyBasicInfo().getEstablishment()));
                if(uploadLocalData.getComponyBasicInfo().getCreateTime()!=null){
                    gsBase.setRegTime(new SimpleDateFormat("yyyy-MM-dd").format(uploadLocalData.getComponyBasicInfo().getEstablishment()));
                }else{
                    gsBase.setRegTime("-");
                }
                
                log.debug("md5----------:" + gsBase.getMd5());
                gsBaseService.save(gsBase);

                    /**公司信息存入base表后，调用接口将此keyNo传给搜索引擎*/
                    try {
                        Map<String, String> keyNo = new HashMap<>();
                        keyNo.put("KeyNo", gsBase.getMd5());
                        String json = HttpUtils.getJsonByGet("http://39.105.108.211:8080/UpdateBaseEs", keyNo);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            } catch (Exception e) {
                log.error("",e);
                log.debug("insert异常原因"+ JSON.toJSON(uploadLocalData));
                return false;
            }
            System.out.println("insert-----------------------------------------" +insertIndex);
            System.out.println("upadte-----------------------------------------" +updateIndex);
        }

        /**银行信息表*/
        GsCorpothersinfo gsCorpothersinfo = new GsCorpothersinfo();
        List<GsCorpothersinfo> md5Cor = gsCorpothersinfoService.getByMd5(gsBase.getMd5());
        for (int i = 0; i < md5Cor.size(); i++) {
            log.debug("开始更新银行信息表："+JSON.toJSON(uploadLocalData));
                try {
                    gsCorpothersinfo = md5Cor.get(i);
                    gsCorpothersinfo.setBank(uploadLocalData.getComponyBasicInfo().getOpeningBank());
                    gsCorpothersinfo.setAccount(uploadLocalData.getComponyBasicInfo().getAccount());
                    gsCorpothersinfo.setContactPersion(uploadLocalData.getComponyBasicInfo().getContact());
                    gsCorpothersinfo.setContact(uploadLocalData.getComponyBasicInfo().getContactTel());
                    gsCorpothersinfo.setCharitable(dealBigDecimal(uploadLocalData.getSocialWelfare().getGiveMoney()));//慈善
                    gsCorpothersinfo.setFiveyearspatents(String.valueOf(uploadLocalData.getPatent().getPantCount()));//近五年专利数量
                    gsCorpothersinfo.setHighlyEnterprises(String.valueOf(uploadLocalData.getPatent().getIsHighTech()));//获得高薪技术产业
                    gsCorpothersinfoService.update(gsCorpothersinfo);
                } catch (Exception e) {
                    log.error("",e);
                    log.debug("更新异常原因"+ JSON.toJSON(uploadLocalData));
                    return false;
                }
            }
        if (md5Cor.size() == 0 && (uploadLocalData.getComponyBasicInfo().getOpeningBank() != null || uploadLocalData.getComponyBasicInfo().getAccount() != null)) {
            try {
                log.debug("开始insert银行信息表："+JSON.toJSON(uploadLocalData));
                gsCorpothersinfo.setBank(uploadLocalData.getComponyBasicInfo().getOpeningBank());
                gsCorpothersinfo.setAccount(uploadLocalData.getComponyBasicInfo().getAccount());
                gsCorpothersinfo.setContactPersion(uploadLocalData.getComponyBasicInfo().getContact());
                gsCorpothersinfo.setContact(uploadLocalData.getComponyBasicInfo().getContactTel());
                gsCorpothersinfo.setCharitable(dealBigDecimal(uploadLocalData.getSocialWelfare().getGiveMoney()));//慈善
                gsCorpothersinfo.setFiveyearspatents(String.valueOf(uploadLocalData.getPatent().getPantCount()));//近五年专利数量
                gsCorpothersinfo.setHighlyEnterprises(String.valueOf(uploadLocalData.getPatent().getIsHighTech()));//获得高薪技术产业
                gsCorpothersinfo.setCoMd5(gsBase.getMd5());
                gsCorpothersinfoService.save(gsCorpothersinfo);
            } catch (Exception e) {
                log.error("",e);
                log.debug("insert异常原因"+ JSON.toJSON(uploadLocalData));
                return false;
            }

        }

        /**变更信息表*/
        GsChange gsChange = new GsChange();
        List<GsChange> md5Change = gsChangeService.getByMd5(gsBase.getMd5());
        List<ChangeRecord> listChange = uploadLocalData.getChangeRecords();
        if(null != listChange) {
            for (int i = 0; i < md5Change.size(); i++) {
                    gsChange.setCoMd5(gsBase.getMd5());
                    gsChangeService.delete(gsChange);
                    for (int j = 0; j < listChange.size(); j++) {
                        log.debug("开始更新GsChange表：" + JSON.toJSON(uploadLocalData));
                        try {
                            gsChange.setChangeBefore(listChange.get(j).getBeforeRecord());
                            gsChange.setChangeAfter(listChange.get(j).getAfterRecord());
                            gsChange.setChangeProject(listChange.get(j).getProject());
                            if (listChange.get(j).getDateApproval() != null) {
                                gsChange.setChangeTime(new SimpleDateFormat("yyyy-MM-dd").format(listChange.get(j).getDateApproval()));
                            } else {
                                gsChange.setChangeTime("-");
                            }
//                        gsChange.setId(Long.valueOf(md5Change.get(i).getId()));
                            gsChangeService.save(gsChange);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                        }
                    }
                }
            for (int j = 0; j < listChange.size(); j++) {
                if (md5Change.size() == 0 && (listChange.get(j).getBeforeRecord() != null || listChange.get(j).getAfterRecord() != null)) {
                    try {
                        log.debug("开始insertGsChange表：" + JSON.toJSON(uploadLocalData));
                        gsChange.setChangeBefore(listChange.get(j).getBeforeRecord());
                        gsChange.setChangeAfter(listChange.get(j).getAfterRecord());
                        gsChange.setChangeProject(listChange.get(j).getProject());
                        if (listChange.get(j).getDateApproval() != null) {
                            gsChange.setChangeTime(new SimpleDateFormat("yyyy-MM-dd").format(listChange.get(j).getDateApproval()));
                        } else {
                            gsChange.setChangeTime("-");
                        }
                        gsChange.setCoMd5(gsBase.getMd5());
//                    gsChange.setMd5(UUID.randomUUID().toString().replace("-", ""));
                        gsChangeService.save(gsChange);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }

                }
            }
        }

        /**分支机构表*/
        GsBranch gsBranch = new GsBranch();
        List<GsBranch> md5Branch = gsBranchService.getByMd5(gsBase.getMd5());
        List<Branch> listBranch = uploadLocalData.getBranches();
        if(null != listBranch){
            for (int i = 0; i < md5Branch.size();i++) {
                    gsBranch.setCoMd5(gsBase.getMd5());
                    gsBranchService.delete(gsBranch);
                    for (int j = 0; j < listBranch.size(); j++) {
                        log.debug("开始更新Branch表："+JSON.toJSON(uploadLocalData));
                        try {
                            gsBranch.setLegal(String.valueOf(listBranch.get(j).getContractTel()));
                            gsBranch.setEnterpriseName(listBranch.get(j).getComponyName());
//                        gsBranch.setId(md5Branch.get(i).getId());
                            gsBranchService.save(gsBranch);
                        } catch (Exception e) {
                            log.error("",e);
                            log.debug("更新异常原因"+ JSON.toJSON(uploadLocalData));
                            return false;
                        }
                    }
                }
            for (int j = 0; j < listBranch.size(); j++) {
                if (md5Branch.size() == 0 && listBranch.get(j).getComponyName() != null) {
                    log.debug("开始insertBranch表："+JSON.toJSON(uploadLocalData));
                    try {
                        gsBranch.setEnterpriseName(listBranch.get(j).getComponyName());
                        gsBranch.setLegal(String.valueOf(listBranch.get(j).getContractTel()));
                        gsBranch.setCoMd5(gsBase.getMd5());
//                            gsBranch.setMd5(UUID.randomUUID().toString().replace("-",""));

                        gsBranchService.save(gsBranch);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }
                }
            }
        }

        /**股东信息表*/
        GsGudong gsGudong = new GsGudong();
        List<GsGudong> md5Gudong = gsGudongService.getByMd5(gsBase.getMd5());
        List<Shareholder> listShareholder = uploadLocalData.getShareholder();
        if(null != listShareholder) {
            for (int i = 0; i < md5Gudong.size(); i++) {
                    gsGudong.setCoMd5(gsBase.getMd5());
                    gsGudongService.delete(gsGudong);
                    for (int j = 0; j < listShareholder.size(); j++) {
                        log.debug("开始更新Gudong表：" + JSON.toJSON(uploadLocalData));
                        try {
                            gsGudong.setShareholder((listShareholder.get(j).getSharholder()));//股东
                            gsGudong.setCapitalPercent(String.valueOf(listShareholder.get(j).getProportion()));//出资比例
                            gsGudong.setConfisCapital(String.valueOf(listShareholder.get(j).getMoney()));//出资金额
                            if (listShareholder.get(j).getCreateTime() != null) {
                                gsGudong.setCapitaltime(new SimpleDateFormat("yyyy-MM-dd").format(listShareholder.get(j).getCreateTime()));//出资时间
                            } else {
                                gsGudong.setCapitaltime("-");
                            }
                            gsGudong.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//                        gsGudong.setId(Long.valueOf(md5Gudong.get(i).getId()));
                            gsGudongService.save(gsGudong);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                        }
                }
            }
            for (int j = 0; j < listShareholder.size(); j++) {
                if (md5Gudong.size() == 0 && (listShareholder.get(j).getMoney() != null || listShareholder.get(j).getSharholder() != null)) {
                    try {
                        log.debug("开始insertGudong表：" + JSON.toJSON(uploadLocalData));
                        gsGudong.setShareholder((listShareholder.get(j).getSharholder()));
                        gsGudong.setCapitalPercent(String.valueOf(listShareholder.get(j).getProportion()));
                        gsGudong.setConfisCapital(String.valueOf(listShareholder.get(j).getMoney()));
                        if (listShareholder.get(j).getCreateTime() != null) {
                            gsGudong.setCapitaltime(new SimpleDateFormat("yyyy-MM-dd").format(listShareholder.get(j).getCreateTime()));//出资时间
                        } else {
                            gsGudong.setCapitaltime("-");
                        }
//                    gsGudong.setCapitalType(String.valueOf(listShareholder.get(j).getSharholder()));
                        gsGudong.setCoMd5(gsBase.getMd5());
//                    gsGudong.setMd5(UUID.randomUUID().toString().replace("-", ""));
                        gsGudongService.save(gsGudong);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }
                }
            }
        }
        /**资质表*/
        GsZiZhi gsZiZhi = new GsZiZhi();
        List<GsZiZhi> md5ZiZhi = gsZiZhiService.getByMd5(gsBase.getMd5());
        List<Qualifications> listQualifications = uploadLocalData.getQualifications();
        if(null != listQualifications) {
            for (int i = 0; i < md5ZiZhi.size(); i++) {
                    gsZiZhi.setCoMd5(gsBase.getMd5());
                    gsZiZhiService.delete(gsZiZhi);
                    for (int j = 0; j < listQualifications.size(); j++) {
                        try {
                            log.debug("开始更新zizhi表：" + JSON.toJSON(uploadLocalData));
                            gsZiZhi.setCert_type(listQualifications.get(j).getLevel());
                            gsZiZhi.setName(listQualifications.get(j).getQualificationsName());
                            gsZiZhi.setCert_number(listQualifications.get(j).getNo());
                            if (listQualifications.get(j).getPublishDate() != null) {
                                gsZiZhi.setPub_time(new SimpleDateFormat("yyyy-MM-dd").format(listQualifications.get(j).getPublishDate()));
                            } else {
                                gsZiZhi.setPub_time("-");
                            }

//                        gsZiZhi.setId(Long.valueOf(md5ZiZhi.get(i).getId()));
                            gsZiZhiService.save(gsZiZhi);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                        }
                }
            }
            for (int j = 0; j < listQualifications.size(); j++) {
                if (md5ZiZhi.size() == 0) {
                    try {
                        log.debug("开始insertzizhi表：" + JSON.toJSON(uploadLocalData));
                        gsZiZhi.setCert_type(listQualifications.get(j).getLevel());
                        gsZiZhi.setName(listQualifications.get(j).getQualificationsName());
                        gsZiZhi.setCert_number(listQualifications.get(j).getNo());
                        if (listQualifications.get(j).getPublishDate() != null) {
                            gsZiZhi.setPub_time(new SimpleDateFormat("yyyy-MM-dd").format(listQualifications.get(j).getPublishDate()));
                        } else {
                            gsZiZhi.setPub_time("-");
                        }
                        gsZiZhi.setCoMd5(gsBase.getMd5());
//                    gsZiZhi.setMd5(UUID.randomUUID().toString().replace("-", ""));
                        gsZiZhiService.save(gsZiZhi);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }

                }
            }
        }
        /**利润表*/
        GsBusinessProfits gsBusinessProfit = new GsBusinessProfits();
        List<GsBusinessProfits> md5Business = gsBusinessProfitsService.getByMd5(gsBase.getMd5());
        List<LrRecord> lrRecordList = uploadLocalData.getLrRecordList();
        if(null != lrRecordList) {
            for (int i = 0; i < md5Business.size(); i++) {
                    gsBusinessProfit.setCoMd5(gsBase.getMd5());
                    gsBusinessProfitsService.delete(gsBusinessProfit);
                    for (int j = 0; j < lrRecordList.size(); j++) {
                        log.debug("开始更新BusinessProfits表：" + JSON.toJSON(uploadLocalData));
                        try {
                            gsBusinessProfit.setYear(lrRecordList.get(j).getYear());
                            gsBusinessProfit.setAssetImpairlosses(dealBigDecimal(lrRecordList.get(j).getZichanjianzhisunshi()));//资产减值损失
                            gsBusinessProfit.setBusinessIncome(dealBigDecimal(lrRecordList.get(j).getYewushouru()));//业务收入
                            gsBusinessProfit.setBusinessProfits(dealBigDecimal(lrRecordList.get(j).getYewulirun()));//业务利润
                            gsBusinessProfit.setBusinessTaxaddTax(dealBigDecimal(lrRecordList.get(j).getYewushuijinyifujia()));//业务税金及附加
                            gsBusinessProfit.setCostOfBusiness(dealBigDecimal(lrRecordList.get(j).getYewuchengben()));//业务成本
                            gsBusinessProfit.setOtherBusinessProfits(dealBigDecimal(lrRecordList.get(j).getQitayewulirun()));//其他业务利润
                            gsBusinessProfit.setOperatingExpenses(dealBigDecimal(lrRecordList.get(j).getYingyefeiyong()));//营业费用
                            gsBusinessProfit.setOperatingProfit(dealBigDecimal(lrRecordList.get(j).getYingyelirun()));//营业利用
                            gsBusinessProfit.setManagementFees(dealBigDecimal(lrRecordList.get(j).getGuanlifeiyong()));//管理费用
                            gsBusinessProfit.setFinanceCharges(dealBigDecimal(lrRecordList.get(j).getCaiwufeiyong()));//财务费用
                            gsBusinessProfit.setIncomeTax(dealBigDecimal(lrRecordList.get(j).getSuodeshui()));//所得税
                            gsBusinessProfit.setInterestPayments(dealBigDecimal(lrRecordList.get(j).getLixizhichu()));//其中：利息支出
                            gsBusinessProfit.setNetProfit(dealBigDecimal(lrRecordList.get(j).getJinglirun()));//净利润
                            gsBusinessProfit.setReturnOnInvestment(dealBigDecimal(lrRecordList.get(j).getTouziliyi()));//加：投资收益
                            gsBusinessProfit.setNonOperateExpenses(dealBigDecimal(lrRecordList.get(j).getYingyewaizhichu()));//减：营业外支出
                            gsBusinessProfit.setNonOperateExpenses(dealBigDecimal(lrRecordList.get(j).getYingyewaishouru()));//加：营业外收入
                            gsBusinessProfit.setProfitTotal(dealBigDecimal(lrRecordList.get(j).getLirunzonge()));//利润总额
                            gsBusinessProfit.setPreProfitLossAdjust(dealBigDecimal(lrRecordList.get(j).getYiqiannaindushunyitiaozheng()));//以前年度损益调整
//                        gsBusinessProfit.setId(Long.valueOf(md5Business.get(i).getId()));
                            gsBusinessProfitsService.save(gsBusinessProfit);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                    }
                }
            }
            for (int j = 0; j < lrRecordList.size(); j++) {
                if (md5Business.size() == 0 && lrRecordList.get(j).getLirunzonge() != null) {
                    log.debug("开始insertBusinessProfits表：" + JSON.toJSON(uploadLocalData));
                    try {
                        gsBusinessProfit.setYear(lrRecordList.get(j).getYear());
                        gsBusinessProfit.setAssetImpairlosses(dealBigDecimal(lrRecordList.get(j).getZichanjianzhisunshi()));//资产减值损失
                        gsBusinessProfit.setBusinessIncome(dealBigDecimal(lrRecordList.get(j).getYewushouru()));//业务收入
                        gsBusinessProfit.setBusinessProfits(dealBigDecimal(lrRecordList.get(j).getYewulirun()));//业务利润
                        gsBusinessProfit.setBusinessTaxaddTax(dealBigDecimal(lrRecordList.get(j).getYewushuijinyifujia()));//业务税金及附加
                        gsBusinessProfit.setCostOfBusiness(dealBigDecimal(lrRecordList.get(j).getYewuchengben()));//业务成本
                        gsBusinessProfit.setOtherBusinessProfits(dealBigDecimal(lrRecordList.get(j).getQitayewulirun()));//其他业务利润
                        gsBusinessProfit.setOperatingExpenses(dealBigDecimal(lrRecordList.get(j).getYingyefeiyong()));//营业费用
                        gsBusinessProfit.setOperatingProfit(dealBigDecimal(lrRecordList.get(j).getYingyelirun()));//营业利用
                        gsBusinessProfit.setManagementFees(dealBigDecimal(lrRecordList.get(j).getGuanlifeiyong()));//管理费用
                        gsBusinessProfit.setFinanceCharges(dealBigDecimal(lrRecordList.get(j).getCaiwufeiyong()));//财务费用
                        gsBusinessProfit.setIncomeTax(dealBigDecimal(lrRecordList.get(j).getSuodeshui()));//所得税
                        gsBusinessProfit.setInterestPayments(dealBigDecimal(lrRecordList.get(j).getLixizhichu()));//其中：利息支出
                        gsBusinessProfit.setNetProfit(dealBigDecimal(lrRecordList.get(j).getJinglirun()));//净利润
                        gsBusinessProfit.setReturnOnInvestment(dealBigDecimal(lrRecordList.get(j).getTouziliyi()));//加：投资收益
                        gsBusinessProfit.setNonOperateExpenses(dealBigDecimal(lrRecordList.get(j).getYingyewaizhichu()));//减：营业外支出
                        gsBusinessProfit.setNonOperateExpenses(dealBigDecimal(lrRecordList.get(j).getYingyewaishouru()));//加：营业外收入
                        gsBusinessProfit.setProfitTotal(dealBigDecimal(lrRecordList.get(j).getLirunzonge()));//利润总额
                        gsBusinessProfit.setPreProfitLossAdjust(dealBigDecimal(lrRecordList.get(j).getYiqiannaindushunyitiaozheng()));//以前年度损益调整
                        gsBusinessProfit.setCoMd5(gsBase.getMd5());
                        gsBusinessProfitsService.save(gsBusinessProfit);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }

                }
            }
        }
        /**现金流量表*/
        GsCashFlow gsCashFlow = new GsCashFlow();
        List<GsCashFlow> gscashFlowsMd5 = gsCashFlowService.getByMd5(gsBase.getMd5());
        List<XjllRecord> xjllList = uploadLocalData.getXjllRecords();
        if(null != xjllList) {
            for (int i = 0; i < gscashFlowsMd5.size(); i++) {
                    gsCashFlow.setCoMd5(gsBase.getMd5());
                    gsCashFlowService.delete(gsCashFlow);
                    for (int j = 0; j < xjllList.size(); j++) {
                        log.debug("开始更新cashfolw表：" + JSON.toJSON(uploadLocalData));
                        try {
                            gsCashFlow.setAallTaxesPaid(dealBigDecimal(xjllList.get(j).getZhifugexiangshuifei()));//支付的各项税费
                            gsCashFlow.setAcashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji1()));//现金流入小计1
                            gsCashFlow.setAcashOutflowsSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuchuxiaoji1()));//现金流出小计1
                            gsCashFlow.setAcashPaid(dealBigDecimal(xjllList.get(j).getGoumaishangpinzhifuxianjin()));//购买商品、接受劳务支付的现金
                            gsCashFlow.setAcashPaidEmployees(dealBigDecimal(xjllList.get(j).getZhifuyuangongzhifuxianjin()));//支付给职工以及为职工支付的现金
                            gsCashFlow.setAcashPaidOthers(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与经营活动有关的现金
                            gsCashFlow.setAcashReceived(dealBigDecimal(xjllList.get(j).getShoudaoxianjin()));//销售商品、提供劳务收到的现金
                            gsCashFlow.setAnetCashFlow(dealBigDecimal(xjllList.get(j).getJingyinghuodongxianjinjine()));//经营活动产生的现金流量净额
                            gsCashFlow.setAotherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与经营活动有关的现金
                            gsCashFlow.setAtaxRefundsReceived(dealBigDecimal(xjllList.get(j).getShoudaoshuifeifanhuan()));//收到的税费返还
                            gsCashFlow.setCashOutflowsubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuchuxiaoji2()));//现金流出小计2
                            gsCashFlow.setCCashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji2()));//现金流入小计2
                            gsCashFlow.setCashPaidlongTermAssets(dealBigDecimal(xjllList.get(j).getGoujianzichanzhifuxianjin()));//购建固定资产、无形资产和其他长期资产所支付的现金
                            gsCashFlow.setCashReceivedInvestments(dealBigDecimal(xjllList.get(j).getShouhuitouzixianjin()));//收回投资所收到的现金
                            gsCashFlow.setCCashReceivedLoan(dealBigDecimal(xjllList.get(j).getJiekuanshoudaoxianjin()));//借款所收到的现金
                            gsCashFlow.setCDistributionCashPaid(dealBigDecimal(xjllList.get(j).getFenpeilirunzhifuxianjin()));//分配股利、利润或偿付利息所支付的现金
                            gsCashFlow.setCEffectExchangeRate(dealBigDecimal(xjllList.get(j).getHuilvbiandongxianjinyingxiang()));//汇率变动对现金的影响
                            gsCashFlow.setCNetCashFlow(dealBigDecimal(xjllList.get(j).getChouzixianjinjinge()));//筹资活动产生的现金流量净额
                            gsCashFlow.setCNetIncreaseCash(dealBigDecimal(xjllList.get(j).getXianjindengjiawujingjiaoyizengjiae()));//现金及现金等价物净增加额
                            gsCashFlow.setCOtherCashDisbursements(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与筹资活动有关的现金
                            gsCashFlow.setCOtherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与筹资活动有关的现金
                            gsCashFlow.setCPaymentCashDebtRepay(dealBigDecimal(xjllList.get(j).getChangzhaizhifuxianjin()));//偿还债务所支付的现金
                            gsCashFlow.setCReceiveCashInvestments(dealBigDecimal(xjllList.get(j).getXishoutouzixianjin()));//吸收投资所收到的现金
                            gsCashFlow.setIotherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与投资活动有关的现金
                            gsCashFlow.setNetCashFlowInvestment(dealBigDecimal(xjllList.get(j).getXianjinliuliangjine()));//投资活动产生的现金流量净额
                            gsCashFlow.setNetCashRecovered(dealBigDecimal(xjllList.get(j).getChangqitouzixianjinjinge()));//处置固定资产、无形资产和其他长期资产所收回的现金净额
                            gsCashFlow.setOutCashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji3()));//现金流入小计3
                            //现金流出小计3
                            gsCashFlow.setOutCashPaidInvestment(dealBigDecimal(xjllList.get(j).getDouzizhifuxianjin()));//投资所支付的现金
                            gsCashFlow.setOutOtherCashPayments(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与投资活动有关的现金
                            gsCashFlow.setReceiveCashInvestmentIncome(dealBigDecimal(xjllList.get(j).getTouziliyixianjin()));//取得投资收益所收到的现金
//                        gsCashFlow.setId(Long.valueOf(gscashFlowsMd5.get(i).getId()));
                            gsCashFlowService.save(gsCashFlow);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                        }
                }
            }
            for (int j = 0; j < xjllList.size(); j++) {
                if (gscashFlowsMd5.size() == 0 && xjllList.get(j).getDouzizhifuxianjin() != null) {
                    log.debug("开始insertcashfolw表：" + JSON.toJSON(uploadLocalData));
                    try {
                        gsCashFlow.setAallTaxesPaid(dealBigDecimal(xjllList.get(j).getZhifugexiangshuifei()));//支付的各项税费
                        gsCashFlow.setAcashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji1()));//现金流入小计1
                        gsCashFlow.setAcashOutflowsSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuchuxiaoji1()));//现金流出小计1
                        gsCashFlow.setAcashPaid(dealBigDecimal(xjllList.get(j).getGoumaishangpinzhifuxianjin()));//购买商品、接受劳务支付的现金
                        gsCashFlow.setAcashPaidEmployees(dealBigDecimal(xjllList.get(j).getZhifuyuangongzhifuxianjin()));//支付给职工以及为职工支付的现金
                        gsCashFlow.setAcashPaidOthers(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与经营活动有关的现金
                        gsCashFlow.setAcashReceived(dealBigDecimal(xjllList.get(j).getShoudaoxianjin()));//销售商品、提供劳务收到的现金
                        gsCashFlow.setAnetCashFlow(dealBigDecimal(xjllList.get(j).getJingyinghuodongxianjinjine()));//经营活动产生的现金流量净额
                        gsCashFlow.setAotherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与经营活动有关的现金
                        gsCashFlow.setAtaxRefundsReceived(dealBigDecimal(xjllList.get(j).getShoudaoshuifeifanhuan()));//收到的税费返还
                        gsCashFlow.setCashOutflowsubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuchuxiaoji2()));//现金流出小计2
                        gsCashFlow.setCCashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji2()));//现金流入小计2
                        gsCashFlow.setCashPaidlongTermAssets(dealBigDecimal(xjllList.get(j).getGoujianzichanzhifuxianjin()));//购建固定资产、无形资产和其他长期资产所支付的现金
                        gsCashFlow.setCashReceivedInvestments(dealBigDecimal(xjllList.get(j).getShouhuitouzixianjin()));//收回投资所收到的现金
                        gsCashFlow.setCCashReceivedLoan(dealBigDecimal(xjllList.get(j).getJiekuanshoudaoxianjin()));//借款所收到的现金
                        gsCashFlow.setCDistributionCashPaid(dealBigDecimal(xjllList.get(j).getFenpeilirunzhifuxianjin()));//分配股利、利润或偿付利息所支付的现金
                        gsCashFlow.setCEffectExchangeRate(dealBigDecimal(xjllList.get(j).getHuilvbiandongxianjinyingxiang()));//汇率变动对现金的影响
                        gsCashFlow.setCNetCashFlow(dealBigDecimal(xjllList.get(j).getChouzixianjinjinge()));//筹资活动产生的现金流量净额
                        gsCashFlow.setCNetIncreaseCash(dealBigDecimal(xjllList.get(j).getXianjindengjiawujingjiaoyizengjiae()));//现金及现金等价物净增加额
                        gsCashFlow.setCOtherCashDisbursements(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与筹资活动有关的现金
                        gsCashFlow.setCOtherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与筹资活动有关的现金
                        gsCashFlow.setCPaymentCashDebtRepay(dealBigDecimal(xjllList.get(j).getChangzhaizhifuxianjin()));//偿还债务所支付的现金
                        gsCashFlow.setCReceiveCashInvestments(dealBigDecimal(xjllList.get(j).getXishoutouzixianjin()));//吸收投资所收到的现金
                        gsCashFlow.setIotherCashReceived(dealBigDecimal(xjllList.get(j).getShoudaoqitahuodongxianjin()));//收到的其他与投资活动有关的现金
                        gsCashFlow.setNetCashFlowInvestment(dealBigDecimal(xjllList.get(j).getXianjinliuliangjine()));//投资活动产生的现金流量净额
                        gsCashFlow.setNetCashRecovered(dealBigDecimal(xjllList.get(j).getChangqitouzixianjinjinge()));//处置固定资产、无形资产和其他长期资产所收回的现金净额
                        gsCashFlow.setOutCashFlowSubtotal(dealBigDecimal(xjllList.get(j).getXianjinliuruxiaoji3()));//现金流入小计3
                        gsCashFlow.setOutCashPaidInvestment(dealBigDecimal(xjllList.get(j).getDouzizhifuxianjin()));//投资所支付的现金
                        gsCashFlow.setOutOtherCashPayments(dealBigDecimal(xjllList.get(j).getZhifuqitajingyehuodongxianjin()));//支付的其他与投资活动有关的现金
                        gsCashFlow.setReceiveCashInvestmentIncome(dealBigDecimal(xjllList.get(j).getTouziliyixianjin()));//取得投资收益所收到的现金
                        gsCashFlow.setCoMd5(gsBase.getMd5());
                        gsCashFlowService.save(gsCashFlow);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }
                }
            }
        }
        /**资产负债表*/
        GsAssetsliab gsAssetsliab = new GsAssetsliab();
        List<GsAssetsliab> assetsMd5 = gsAssetsliabService.getByMd5(gsBase.getMd5());
        List<ZcfzRecord> zcfzRecords = uploadLocalData.getZcfzRecords();
        if(null != zcfzRecords) {
            for (int i = 0; i < assetsMd5.size(); i++) {//循环资产负债表中所有的MD5
                    gsAssetsliab.setCoMd5(gsBase.getMd5());
                    gsAssetsliabService.delete(gsAssetsliab);
                    //如果主表MD5和子表MD5有相同记录，则更新
                    for (int j = 0; j < zcfzRecords.size(); j++) {//循环传进来的数据
                        log.debug("开始更新assetsliab表：" + JSON.toJSON(uploadLocalData));
//                    List<GsAssetsliab> year = gsAssetsliabService.getByYear(zcfzRecords.get(j).getYear());
                        try {
                            gsAssetsliab.setFMonetaryFund(dealBigDecimal(zcfzRecords.get(j).getHuobijijin()));//流动资产-货币资金
                            gsAssetsliab.setFTranslFinanAssets(dealBigDecimal(zcfzRecords.get(j).getJiaoyixingjinrongzichan()));//流动资产-交易性金融资产
                            gsAssetsliab.setFNotesReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshoupiaojv()));//流动资产-应收票据
                            gsAssetsliab.setFAccountReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshouzhangkuan()));//流动资产-应收帐款
                            gsAssetsliab.setFPrepayments(dealBigDecimal(zcfzRecords.get(j).getYufukuanxiang()));//流动资产-预付款项
                            gsAssetsliab.setFInterestReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshoulixi()));//流动资产-应收利息
                            gsAssetsliab.setFDividendsReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshouguli()));//流动资产-应收股利
                            gsAssetsliab.setFOtherReceivables(dealBigDecimal(zcfzRecords.get(j).getQitashoukuan()));//流动资产-其他应收款
                            gsAssetsliab.setFInventory(dealBigDecimal(zcfzRecords.get(j).getCunhuo()));//流动资产-存货
                            gsAssetsliab.setFPrepaidExpenses(dealBigDecimal(zcfzRecords.get(j).getDaitanfeiyong()));//流动资产-待摊费用
                            gsAssetsliab.setFYearNonAssets(dealBigDecimal(zcfzRecords.get(j).getYinianneidaoqiliudongzichan()));//流动资产一年内到期的非流动资产
                            gsAssetsliab.setFOtherCurrentAssets(dealBigDecimal(zcfzRecords.get(j).getQitaliudongzichan()));//流动资产-其他流动资产
                            gsAssetsliab.setFTotalCurrentAssets(dealBigDecimal(zcfzRecords.get(j).getLiudongzichanheji()));//流动资产-流动资产合计
                            gsAssetsliab.setUMaturityInvestment(dealBigDecimal(zcfzRecords.get(j).getChiyouzhidaoqitouzi()));//非流动资产-持有至到期投资
                            gsAssetsliab.setULongTermReceivables(dealBigDecimal(zcfzRecords.get(j).getChangqiyingshoukuan()));//非流动资产-长期应收款
                            gsAssetsliab.setULongTermEquityInvest(dealBigDecimal(zcfzRecords.get(j).getChangqiguquantouzi()));//非流动资产-长期股权投资
                            gsAssetsliab.setUInvestmentProperty(dealBigDecimal(zcfzRecords.get(j).getTouzixingfangdichan()));//非流动资产-投资性房地产
                            gsAssetsliab.setUOriginalFixedAssets(dealBigDecimal(zcfzRecords.get(j).getGudingzhichanyuanzhi()));//非流动资产-固定资产原值
                            gsAssetsliab.setUAccumulatedDepreciation(dealBigDecimal(zcfzRecords.get(j).getLeijizhejiu()));//非流动资产-减：累计折旧
                            gsAssetsliab.setUNetFixedAssets(dealBigDecimal(zcfzRecords.get(j).getGudingzhichanjingzhi()));//非流动资产-固定资产净值
                            gsAssetsliab.setUProjectsUnderConstruct(dealBigDecimal(zcfzRecords.get(j).getZaijiangongcheng()));//非流动资产-在建工程
                            gsAssetsliab.setUEngineeringMaterials(dealBigDecimal(zcfzRecords.get(j).getGongchengwuzi()));//非流动资产-工程物资
                            gsAssetsliab.setUIntangibleAssets(dealBigDecimal(zcfzRecords.get(j).getWuxingzichan()));//非流动资产-无形资产
                            gsAssetsliab.setUDevelopmentSpend(dealBigDecimal(zcfzRecords.get(j).getKaifazhichu()));//非流动资产-开发支出
                            gsAssetsliab.setULongTermDeferredExpenses(dealBigDecimal(zcfzRecords.get(j).getChangqidaitanfen()));//非流动资产-长期待摊费用
                            gsAssetsliab.setUOtherNonAssets(dealBigDecimal(zcfzRecords.get(j).getQitafeiliudongzichan()));//非流动资产-其他非流动资产
                            gsAssetsliab.setUTotalNonAssets(dealBigDecimal(zcfzRecords.get(j).getFeiliudongzichanheji()));//非流动资产合计
                            gsAssetsliab.setAssetsTotal(dealBigDecimal(zcfzRecords.get(j).getZichanheji()));//资产合计
                            gsAssetsliab.setFfShortTermBorrow(dealBigDecimal(zcfzRecords.get(j).getDuanqijiekuan()));//流动负债-短期借款
                            gsAssetsliab.setFfTransFinanliab(dealBigDecimal(zcfzRecords.get(j).getJiaoyixingjinrongfuzai()));//流动负债-交易性金融负债
                            gsAssetsliab.setFfNotesPayable(dealBigDecimal(zcfzRecords.get(j).getYingfupiaojv()));//流动负债-应付票据
                            gsAssetsliab.setFfAccountsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhangkuan()));//流动负债-应付帐款
                            gsAssetsliab.setFfAdvancePayment(dealBigDecimal(zcfzRecords.get(j).getYushoukuanxiang()));//流动负债-预收款项
                            gsAssetsliab.setFfPayPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhigongxinchou()));//流动负债-应付职工薪酬
                            gsAssetsliab.setFfPayableTaxes(dealBigDecimal(zcfzRecords.get(j).getYingjiaofeishui()));//流动负债-应交税费
                            gsAssetsliab.setFfInterestPayable(dealBigDecimal(zcfzRecords.get(j).getYingfulixi()));//流动负债-应付利息
                            gsAssetsliab.setFfDividendsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuguli()));//流动负债-应付股利
                            gsAssetsliab.setFfOtherPayables(dealBigDecimal(zcfzRecords.get(j).getQitayingfukuan()));//流动负债-其他应付款
                            gsAssetsliab.setFfNonYearliab(dealBigDecimal(zcfzRecords.get(j).getYinianneidaoqidefeiliudongfuzhai()));//流动负债-一年内到期的非流动负债
                            gsAssetsliab.setFfOtherCurrentliab(dealBigDecimal(zcfzRecords.get(j).getQitaliudongfuzhai())); //流动负债-其他流动负债
                            gsAssetsliab.setFfTotalCurrentliab(dealBigDecimal(zcfzRecords.get(j).getLiudongfuzhaiheji()));//流动负债-流动负债合计
                            gsAssetsliab.setNffLongTermBorrow(dealBigDecimal(zcfzRecords.get(j).getChangqijiekuan()));//非流动负债-长期借款
                            gsAssetsliab.setNffBondsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhaiquan())); //非流动负债-应付债券
                            gsAssetsliab.setNffLongTermPayable(dealBigDecimal(zcfzRecords.get(j).getChangqiyingfukuan()));//非流动负债-长期应付款
                            gsAssetsliab.setNffSpecialPayables(dealBigDecimal(zcfzRecords.get(j).getZhuanxingyingfukuan()));//非流动负债-专项应付款
                            gsAssetsliab.setNffEstimatedDebts(dealBigDecimal(zcfzRecords.get(j).getYujifuzhai()));//非流动负债-预计负债
                            gsAssetsliab.setNffDeferredTaxLiab(dealBigDecimal(zcfzRecords.get(j).getDiyansuodeshuizichan()));//非流动负债-递延所得税负债
                            gsAssetsliab.getNffOtherNonliab(dealBigDecimal(zcfzRecords.get(j).getQitafeikiudongfuzhai())); //非流动负债-其他非流动负债
                            gsAssetsliab.setNffTotalNonliab(dealBigDecimal(zcfzRecords.get(j).getFeiliudongfuzhaiheji()));//非流动负债-非流动负债合计
                            gsAssetsliab.setNffLiabInTotal(dealBigDecimal(zcfzRecords.get(j).getFuzhaizongji()));//非流动负债-负债总计
                            gsAssetsliab.setOPaidInCapital(dealBigDecimal(zcfzRecords.get(j).getShishouziben()));// 所有者权益-实收资本
                            gsAssetsliab.setOCapitalReserves(dealBigDecimal(zcfzRecords.get(j).getZibengongji())); //所有者权益-资本公积
                            gsAssetsliab.setOSurplusReserves(dealBigDecimal(zcfzRecords.get(j).getYingyugongji()));//  所有者权益-盈余公积
                            gsAssetsliab.setOUndistributedProfit(dealBigDecimal(zcfzRecords.get(j).getWeifenpeilirun())); //所有者权益-未分配利润
                            gsAssetsliab.setOTotalOwnersEquity(dealBigDecimal(zcfzRecords.get(j).getSuoyouzhequanyiheji())); // 所有者权益-所有者权益合计
                            gsAssetsliab.setOLiabOwnEquityTotal(dealBigDecimal(zcfzRecords.get(j).getFuzhaihesuoyouzhequanyi()));//  所有者权益-负债和所有者权益总计
                            gsAssetsliab.setYear(zcfzRecords.get(j).getYear());//  年份
//                                gsAssetsliab.setId(Long.valueOf(assetsMd5.get(i).getId()));
                            gsAssetsliabService.save(gsAssetsliab);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                    }
                }
            }
            for (int j = 0; j < zcfzRecords.size(); j++) { //循环传进来的数据
//            List<GsAssetsliab> year = gsAssetsliabService.getByYear(zcfzRecords.get(j).getYear());
                if (assetsMd5.size() == 0 && zcfzRecords.get(j).getYear() != null) {
                    log.debug("开始insertassetsliab表：" + JSON.toJSON(uploadLocalData));
                    try {
                        gsAssetsliab.setFMonetaryFund(dealBigDecimal(zcfzRecords.get(j).getHuobijijin()));//流动资产-货币资金
                        gsAssetsliab.setFTranslFinanAssets(dealBigDecimal(zcfzRecords.get(j).getJiaoyixingjinrongzichan()));//流动资产-交易性金融资产
                        gsAssetsliab.setFNotesReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshoupiaojv()));//流动资产-应收票据
                        gsAssetsliab.setFAccountReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshouzhangkuan()));//流动资产-应收帐款
                        gsAssetsliab.setFPrepayments(dealBigDecimal(zcfzRecords.get(j).getYufukuanxiang()));//流动资产-预付款项
                        gsAssetsliab.setFInterestReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshoulixi()));//流动资产-应收利息
                        gsAssetsliab.setFDividendsReceivable(dealBigDecimal(zcfzRecords.get(j).getYingshouguli()));//流动资产-应收股利
                        gsAssetsliab.setFOtherReceivables(dealBigDecimal(zcfzRecords.get(j).getQitashoukuan()));//流动资产-其他应收款
                        gsAssetsliab.setFInventory(dealBigDecimal(zcfzRecords.get(j).getCunhuo()));//流动资产-存货
                        gsAssetsliab.setFPrepaidExpenses(dealBigDecimal(zcfzRecords.get(j).getDaitanfeiyong()));//流动资产-待摊费用
                        gsAssetsliab.setFYearNonAssets(dealBigDecimal(zcfzRecords.get(j).getYinianneidaoqiliudongzichan()));//流动资产一年内到期的非流动资产
                        gsAssetsliab.setFOtherCurrentAssets(dealBigDecimal(zcfzRecords.get(j).getQitaliudongzichan()));//流动资产-其他流动资产
                        gsAssetsliab.setFTotalCurrentAssets(dealBigDecimal(zcfzRecords.get(j).getLiudongzichanheji()));//流动资产-流动资产合计
                        gsAssetsliab.setUMaturityInvestment(dealBigDecimal(zcfzRecords.get(j).getChiyouzhidaoqitouzi()));//非流动资产-持有至到期投资
                        gsAssetsliab.setULongTermReceivables(dealBigDecimal(zcfzRecords.get(j).getChangqiyingshoukuan()));//非流动资产-长期应收款
                        gsAssetsliab.setULongTermEquityInvest(dealBigDecimal(zcfzRecords.get(j).getChangqiguquantouzi()));//非流动资产-长期股权投资
                        gsAssetsliab.setUInvestmentProperty(dealBigDecimal(zcfzRecords.get(j).getTouzixingfangdichan()));//非流动资产-投资性房地产
                        gsAssetsliab.setUOriginalFixedAssets(dealBigDecimal(zcfzRecords.get(j).getGudingzhichanyuanzhi()));//非流动资产-固定资产原值
                        gsAssetsliab.setUAccumulatedDepreciation(dealBigDecimal(zcfzRecords.get(j).getLeijizhejiu()));//非流动资产-减：累计折旧
                        gsAssetsliab.setUNetFixedAssets(dealBigDecimal(zcfzRecords.get(j).getGudingzhichanjingzhi()));//非流动资产-固定资产净值
                        gsAssetsliab.setUProjectsUnderConstruct(dealBigDecimal(zcfzRecords.get(j).getZaijiangongcheng()));//非流动资产-在建工程
                        gsAssetsliab.setUEngineeringMaterials(dealBigDecimal(zcfzRecords.get(j).getGongchengwuzi()));//非流动资产-工程物资
                        gsAssetsliab.setUIntangibleAssets(dealBigDecimal(zcfzRecords.get(j).getWuxingzichan()));//非流动资产-无形资产
                        gsAssetsliab.setUDevelopmentSpend(dealBigDecimal(zcfzRecords.get(j).getKaifazhichu()));//非流动资产-开发支出
                        gsAssetsliab.setULongTermDeferredExpenses(dealBigDecimal(zcfzRecords.get(j).getChangqidaitanfen()));//非流动资产-长期待摊费用
                        gsAssetsliab.setUOtherNonAssets(dealBigDecimal(zcfzRecords.get(j).getQitafeiliudongzichan()));//非流动资产-其他非流动资产
                        gsAssetsliab.setUTotalNonAssets(dealBigDecimal(zcfzRecords.get(j).getFeiliudongzichanheji()));//非流动资产合计
                        gsAssetsliab.setAssetsTotal(dealBigDecimal(zcfzRecords.get(j).getZichanheji()));//资产合计
                        gsAssetsliab.setFfShortTermBorrow(dealBigDecimal(zcfzRecords.get(j).getDuanqijiekuan()));//流动负债-短期借款
                        gsAssetsliab.setFfTransFinanliab(dealBigDecimal(zcfzRecords.get(j).getJiaoyixingjinrongfuzai()));//流动负债-交易性金融负债
                        gsAssetsliab.setFfNotesPayable(dealBigDecimal(zcfzRecords.get(j).getYingfupiaojv()));//流动负债-应付票据
                        gsAssetsliab.setFfAccountsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhangkuan()));//流动负债-应付帐款
                        gsAssetsliab.setFfAdvancePayment(dealBigDecimal(zcfzRecords.get(j).getYushoukuanxiang()));//流动负债-预收款项
                        gsAssetsliab.setFfPayPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhigongxinchou()));//流动负债-应付职工薪酬
                        gsAssetsliab.setFfPayableTaxes(dealBigDecimal(zcfzRecords.get(j).getYingjiaofeishui()));//流动负债-应交税费
                        gsAssetsliab.setFfInterestPayable(dealBigDecimal(zcfzRecords.get(j).getYingfulixi()));//流动负债-应付利息
                        gsAssetsliab.setFfDividendsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuguli()));//流动负债-应付股利
                        gsAssetsliab.setFfOtherPayables(dealBigDecimal(zcfzRecords.get(j).getQitayingfukuan()));//流动负债-其他应付款
                        gsAssetsliab.setFfNonYearliab(dealBigDecimal(zcfzRecords.get(j).getYinianneidaoqidefeiliudongfuzhai()));//流动负债-一年内到期的非流动负债
                        gsAssetsliab.setFfOtherCurrentliab(dealBigDecimal(zcfzRecords.get(j).getQitaliudongfuzhai())); //流动负债-其他流动负债
                        gsAssetsliab.setFfTotalCurrentliab(dealBigDecimal(zcfzRecords.get(j).getLiudongfuzhaiheji()));//流动负债-流动负债合计
                        gsAssetsliab.setNffLongTermBorrow(dealBigDecimal(zcfzRecords.get(j).getChangqijiekuan()));//非流动负债-长期借款
                        gsAssetsliab.setNffBondsPayable(dealBigDecimal(zcfzRecords.get(j).getYingfuzhaiquan())); //非流动负债-应付债券
                        gsAssetsliab.setNffLongTermPayable(dealBigDecimal(zcfzRecords.get(j).getChangqiyingfukuan()));//非流动负债-长期应付款
                        gsAssetsliab.setNffSpecialPayables(dealBigDecimal(zcfzRecords.get(j).getZhuanxingyingfukuan()));//非流动负债-专项应付款
                        gsAssetsliab.setNffEstimatedDebts(dealBigDecimal(zcfzRecords.get(j).getYujifuzhai()));//非流动负债-预计负债
                        gsAssetsliab.setNffDeferredTaxLiab(dealBigDecimal(zcfzRecords.get(j).getDiyansuodeshuizichan()));//非流动负债-递延所得税负债
                        gsAssetsliab.getNffOtherNonliab(dealBigDecimal(zcfzRecords.get(j).getQitafeikiudongfuzhai())); //非流动负债-其他非流动负债
                        gsAssetsliab.setNffTotalNonliab(dealBigDecimal(zcfzRecords.get(j).getFeiliudongfuzhaiheji()));//非流动负债-非流动负债合计
                        gsAssetsliab.setNffLiabInTotal(dealBigDecimal(zcfzRecords.get(j).getFuzhaizongji()));//非流动负债-负债总计
                        gsAssetsliab.setOPaidInCapital(dealBigDecimal(zcfzRecords.get(j).getShishouziben()));// 所有者权益-实收资本
                        gsAssetsliab.setOCapitalReserves(dealBigDecimal(zcfzRecords.get(j).getZibengongji())); //所有者权益-资本公积
                        gsAssetsliab.setOSurplusReserves(dealBigDecimal(zcfzRecords.get(j).getYingyugongji()));//  所有者权益-盈余公积
                        gsAssetsliab.setOUndistributedProfit(dealBigDecimal(zcfzRecords.get(j).getWeifenpeilirun())); //所有者权益-未分配利润
                        gsAssetsliab.setOTotalOwnersEquity(dealBigDecimal(zcfzRecords.get(j).getSuoyouzhequanyiheji())); // 所有者权益-所有者权益合计
                        gsAssetsliab.setOLiabOwnEquityTotal(dealBigDecimal(zcfzRecords.get(j).getFuzhaihesuoyouzhequanyi()));//  所有者权益-负债和所有者权益总计
                        gsAssetsliab.setYear(zcfzRecords.get(j).getYear());//  年份
                        gsAssetsliab.setCoMd5(gsBase.getMd5());
                        gsAssetsliabService.save(gsAssetsliab);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }
                }
            }
        }

        /**高管*/
        GsGaoGuan gsGaoGuan = new GsGaoGuan();
      
        List<Senior> seniors = uploadLocalData.getSenior();
        if(null != seniors) {
            
        	        if(gsBase.getMd5()!=null&&!gsBase.getMd5().equals(""))
        	        {
                    gsGaoGuan.setCoMd5(gsBase.getMd5());
                    gsGaoGuanService.delete(gsGaoGuan);
                    for (int j = 0; j < seniors.size(); j++) {
                        log.debug("开始更新gaoguan表：" + JSON.toJSON(uploadLocalData));
                        try {
                            gsGaoGuan.setAge(String.valueOf(seniors.get(j).getBirthday()));//年龄
                            gsGaoGuan.setEducation(String.valueOf(seniors.get(j).getLevel()));//学历
                            gsGaoGuan.setName(seniors.get(j).getSeniorName());//姓名
                            gsGaoGuan.setPost(String.valueOf(seniors.get(j).getTitle()));//职务
//                        gsGaoGuan.setId(Long.valueOf(gaoGuanMd5.get(i).getId()));
//                    gsGaoGuan.setSharesNumber(dealBigDecimal(uploadLocalData.getShareholder().get(j).getMoney()));//持股数
                            gsGaoGuanService.save(gsGaoGuan);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                    }
                    }
                }

        }
        

        /**合同抽取情况*/
        GsContracts gsContracts = new GsContracts();
        List<GsContracts> contractsMd5 = gsContractsService.getByMd5(gsBase.getMd5());
        ContractExtraction contractExtractions = uploadLocalData.getContractextraction();
        if(null != contractExtractions) {
            for (int i = 0; i < contractsMd5.size(); i++) {
                log.debug("开始更新contracts表：" + JSON.toJSON(uploadLocalData));
                    try {
                        gsContracts.setContactCount(String.valueOf(contractExtractions.getOutProvince() + String.valueOf(contractExtractions.getOutCity() + String.valueOf(contractExtractions.getInCity()))));//合同抽取数量
                        gsContractsService.update(gsContracts);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                }
            }
            if (contractsMd5.size() == 0 && contractExtractions.getOutProvince() != null) {
                try {
                    gsContracts.setCoMd5(gsBase.getMd5());
                    gsContracts.setContactCount(String.valueOf(contractExtractions.getOutProvince() + String.valueOf(contractExtractions.getOutCity() + String.valueOf(contractExtractions.getInCity()))));//合同抽取数量
                    gsContractsService.save(gsContracts);
                } catch (Exception e) {
                    log.error("", e);
                    log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                    return false;
                }
            }
        }
        /**从业人员表*/
        GsEmployees gsEmployees = new GsEmployees();
        List<GsEmployees> empployeesMd5 = gsEmployeesService.getByMd5(gsBase.getMd5());
        EmployeesSituation employeesSituations = uploadLocalData.getEmployeessituation();
        if(null != employeesSituations) {
            for (int i = 0; i < empployeesMd5.size(); i++) {
                log.debug("开始insertemployees表：" + JSON.toJSON(uploadLocalData));
                    gsEmployees.setCoMd5(gsBase.getMd5());
                    gsEmployeesService.delete(gsEmployees);
                    try {
                        gsEmployees.setBkCount(employeesSituations.getUndergraduate());
                        gsEmployees.setBkUpCount(employeesSituations.getUndergraduateUp());
                        gsEmployees.setFiveYearsUpCount(employeesSituations.getFiveUp());
                        gsEmployees.setLowCount(employeesSituations.getPrimaryLevel());
                        gsEmployees.setMiddleCount(employeesSituations.getIntermediate());
                        gsEmployees.setRegCount(employeesSituations.getRegistCount());
                        gsEmployees.setSeniorCount(employeesSituations.getSenior());
                        gsEmployees.setTotalCount(employeesSituations.getEmployeeCount());
                        gsEmployees.setTwoYearsLowCount(employeesSituations.getTwoLow());
                        gsEmployees.setTwoYearsUpCount(employeesSituations.getTwoFive());
                        gsEmployees.setZkCount(employeesSituations.getSpecialty());
                        gsEmployeesService.save(gsEmployees);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                }
            }
            if (empployeesMd5.size() == 0 && employeesSituations.getEmployeeCount() != null) {
                try {
                    log.debug("开始insert employees表：" + JSON.toJSON(uploadLocalData));
                    gsEmployees.setBkCount(employeesSituations.getUndergraduate());
                    gsEmployees.setBkUpCount(employeesSituations.getUndergraduateUp());
                    gsEmployees.setFiveYearsUpCount(employeesSituations.getFiveUp());
                    gsEmployees.setLowCount(employeesSituations.getPrimaryLevel());
                    gsEmployees.setMiddleCount(employeesSituations.getIntermediate());
                    gsEmployees.setRegCount(employeesSituations.getRegistCount());
                    gsEmployees.setSeniorCount(employeesSituations.getSenior());
                    gsEmployees.setTotalCount(employeesSituations.getEmployeeCount());
                    gsEmployees.setTwoYearsLowCount(employeesSituations.getTwoLow());
                    gsEmployees.setTwoYearsUpCount(employeesSituations.getTwoFive());
                    gsEmployees.setZkCount(employeesSituations.getSpecialty());
                    gsEmployees.setCoMd5(gsBase.getMd5());
                    gsEmployeesService.save(gsEmployees);
                } catch (Exception e) {
                    log.error("", e);
                    log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                    return false;
                }

            }
        }

        /**体系认证*/
        GsCertificates gsCertificates = new GsCertificates();
        List<GsCertificates> certificatesMd5 = gsCertificatesService.getByMd5(gsBase.getMd5());
        List<SystemAuthentication> systemauthentication = uploadLocalData.getSystemauthentication();

        if(null != systemauthentication){
            for (int i = 0; i < certificatesMd5.size(); i++) {
                gsCertificates=new GsCertificates();
                log.debug("开始更新systemauthentication表："+JSON.toJSON(uploadLocalData));
                    gsCertificates.setCoMd5(gsBase.getMd5());
                    gsCertificatesService.delete(gsCertificates);
                    for (int j = 0; j < systemauthentication.size(); j++) {
                        try {
                            gsCertificates.setCertName(String.valueOf(systemauthentication.get(j).getType()));
                            gsCertificates.setCertNo(systemauthentication.get(j).getNo());
                            gsCertificates.setCertOrg(systemauthentication.get(j).getOrg());
                            if(systemauthentication.get(j).getLimitDate()!=null){
                                gsCertificates.setCertTime(new SimpleDateFormat("yyyy-MM-dd").format(systemauthentication.get(j).getLimitDate()));
                            }else{
                                gsCertificates.setCertTime("-");
                            }
//                        gsCertificates.setId(Long.valueOf(certificatesMd5.get(i).getId()));
                            gsCertificatesService.save(gsCertificates);
                        } catch (Exception e) {
                            log.error("",e);
                            log.debug("更新异常原因"+ JSON.toJSON(uploadLocalData));
                            return false;
                    }
                }
            }
            for (int j = 0; j < systemauthentication.size(); j++) {
                gsCertificates=new GsCertificates();
                if (certificatesMd5.size() == 0 && systemauthentication.get(j).getNo() != null) {
                    log.debug("开始insert systemauthentication表："+JSON.toJSON(uploadLocalData));
                    try {
                        gsCertificates.setCertName(String.valueOf(systemauthentication.get(j).getType()));
                        gsCertificates.setCertNo(systemauthentication.get(j).getNo());
                        gsCertificates.setCertOrg(systemauthentication.get(j).getOrg());
                        if(systemauthentication.get(j).getLimitDate()!=null){
                            gsCertificates.setCertTime(new SimpleDateFormat("yyyy-MM-dd").format(systemauthentication.get(j).getLimitDate()));
                        }else{
                            gsCertificates.setCertTime("-");
                        }
                    } catch (Exception e) {
                        log.error("",e);
                        log.debug("insert异常原因"+ JSON.toJSON(uploadLocalData));
                        return false;
                    }

                }
            }
        }

        /**企业荣誉*/
        GsHonors gsHonors = new GsHonors();
        List<GsHonors> honorsMd5 = gsHonorsService.getByMd5(gsBase.getMd5());
        List<Honor> honors = uploadLocalData.getHonor();
        if(null != honors) {
            for (int i = 0; i < honorsMd5.size(); i++) {
                log.debug("开始更新honors表：" + JSON.toJSON(uploadLocalData));
                    gsHonors.setCoMd5(gsBase.getMd5());
                    gsHonorsService.delete(gsHonors);
                    for (int j = 0; j < honors.size(); j++) {
                        try {
                            gsHonors.setHonorName(honors.get(j).getAward());
                            gsHonors.setHonorOrg(honors.get(j).getIssuedDep());
                            if (honors.get(j).getIssuedDate() != null) {
                                gsHonors.setHonorTime(new SimpleDateFormat("yyyy-MM-dd").format(honors.get(j).getIssuedDate()));
                            } else {
                                gsHonors.setHonorTime("-");
                            }
                            gsHonors.setProvince(String.valueOf(honors.get(j).getLevel()));
//                        gsHonors.setId(Long.valueOf(honorsMd5.get(i).getId()));
                            gsHonorsService.save(gsHonors);
                        } catch (Exception e) {
                            log.error("", e);
                            log.debug("更新异常原因" + JSON.toJSON(uploadLocalData));
                            return false;
                    }
                }
            }
            for (int j = 0; j < honors.size(); j++) {
                log.debug("开始insert honors表：" + JSON.toJSON(uploadLocalData));
                if (honorsMd5.size() == 0 && honors.get(j).getAward() != null) {
                    try {
                        gsHonors.setHonorName(honors.get(j).getAward());
                        gsHonors.setHonorOrg(honors.get(j).getIssuedDep());
                        if (honors.get(j).getIssuedDate() != null) {
                            gsHonors.setHonorTime(new SimpleDateFormat("yyyy-MM-dd").format(honors.get(j).getIssuedDate()));
                        } else {
                            gsHonors.setHonorTime("-");
                        }
                        gsHonors.setProvince(String.valueOf(honors.get(j).getLevel()));
                        gsHonors.setCoMd5(gsBase.getMd5());
                        gsHonorsService.save(gsHonors);
                    } catch (Exception e) {
                        log.error("", e);
                        log.debug("insert异常原因" + JSON.toJSON(uploadLocalData));
                        return false;
                    }
                }
            }
        }

        /**日志*/
        GsReportLog gsReportLog = new GsReportLog();
        gsReportLog.setKeyNo(gsBase.getMd5());//keyNo
        gsReportLog.setName(uploadLocalData.getComponyBasicInfo().getComponyName());//公司名称
        gsReportLog.setDataRecord(JSON.toJSON(uploadLocalData).toString());//请求数据
        Date date = new Date();
        gsReportLog.setUpdateTime(date.toLocaleString());
        gsReportLogService.save(gsReportLog);


        return true;
    }
}
